package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.PracticeTask;
import com.xmy.cultivate.service.IPracticeTaskService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 备课练课任务 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-11
 */
@RestController
@RequestMapping("/api/practiceTask")
public class PracticeTaskController {

    @Autowired
    IPracticeTaskService iPracticeTaskService;

    @GetMapping("")
    @ApiOperation("查询任务")
    public Result searchTask(@RequestParam Map reMap) {
        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        Page<PracticeTask> iPage = new Page<>(pageNum, pageSize);

        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String taskStartDate = (String) reMap.get("taskStartDate");
        String taskEndDate = (String) reMap.get("taskEndDate");
        String lessonNum = (String) reMap.get("lessonNum");
        String subjectsId = (String) reMap.get("subjectsId");
        String yearClassId = (String) reMap.get("yearClassId");
        String classTypeId = (String) reMap.get("classTypeId");

        QueryWrapper<PracticeTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(lessonNum), "lesson_num", lessonNum);
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        queryWrapper.eq(StringUtils.isNotBlank(classTypeId), "class_type_id", classTypeId);
        queryWrapper.le(StringUtils.isNotBlank(taskStartDate), "task_start_date", taskStartDate);
        queryWrapper.ge(StringUtils.isNotBlank(taskEndDate), "task_end_date", taskEndDate);
        queryWrapper.orderByDesc("lesson_num", "id");
        iPracticeTaskService.page(iPage, queryWrapper);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), Code.GET_OK_MSG);
    }

    @PostMapping("")
    @ApiOperation("添加/修改任务")
    public Result createTask(@RequestBody PracticeTask practiceTask) {
        if (practiceTask.getId() != null) {
            PracticeTask PracticeTask = iPracticeTaskService.getById(practiceTask.getId());
            if (Objects.isNull(PracticeTask)) {
                return new Result(Code.EXP_ERROR, null, "该任务不存在");
            }
        }

        if (practiceTask.getSubjectsId() == null) {
            practiceTask.setSubjectsId(0L);
        }

        QueryWrapper<PracticeTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", practiceTask.getYearPart());
        queryWrapper.eq("quarter_num", practiceTask.getQuarterNum());
        queryWrapper.eq("subjects_id", practiceTask.getSubjectsId());
        queryWrapper.eq("lesson_num", practiceTask.getLessonNum());
        queryWrapper.ne(practiceTask.getId() != null,"id", practiceTask.getId());

        Long count = iPracticeTaskService.count(queryWrapper);
        if (count > 0L) {
            return new Result(Code.ERROR, null, "已有相同课次，请勿重复添加！");
        }

        iPracticeTaskService.saveOrUpdate(practiceTask);

        return new Result(Code.OK, null, Code.OK_MSG);
    }

    @DeleteMapping("")
    @ApiOperation("删除任务")
    @ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "String")
    public Result deleteTask(@RequestParam(value = "id") String id) {
        PracticeTask PracticeTask = iPracticeTaskService.getById(id);
        if (Objects.isNull(PracticeTask)) {
            return new Result(Code.ERROR, null, "该任务不存在");
        }
        iPracticeTaskService.removeById(id);
        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }


}
